package com.cskaoyan.controller;

import com.cskaoyan.bean.promotion.Topic;
import com.cskaoyan.service.promotion.TopicService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.promotion.TopicListVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @autyor:caicaicai
 */
@RestController
public class TopicController {

    @Autowired
    TopicService topicService;

    //?page=1&limit=20&title=%E8%B6%B3&subtitle=%E8%B6%B3&sort=add_time&order=desc
    @RequestMapping("/admin/topic/list")
    @RequiresPermissions("admin:topic:list")
    public BaseRespVo list(Integer page, Integer limit, String sort, String order, String title, String subtitle){
        TopicListVo topicListVo = topicService.list(page, limit, sort, order, title, subtitle);
        return BaseRespVo.ok(topicListVo);
    }

    @RequestMapping("/admin/topic/create")
    @RequiresPermissions("admin:topic:create")
    public BaseRespVo create(@RequestBody Topic topic){
        Topic topicVo = topicService.create(topic);
        return BaseRespVo.ok(topicVo);
    }

    @RequestMapping("/admin/topic/update")
    @RequiresPermissions("admin:topic:update")
    public BaseRespVo update(@RequestBody Topic topic) {
        int code = topicService.update(topic);
        if (code != 200) {
            //return BaseRespVo.fail("编辑失败！");
            return BaseRespVo.error(402,"编辑失败");
        }
        return BaseRespVo.ok(topic);
    }

    @RequestMapping("/admin/topic/delete")
    @RequiresPermissions("admin:topic:delete")
    public BaseRespVo delete(@RequestBody Topic topic){
        int code = topicService.delete(topic);
        if (code != 200) {
            //return BaseRespVo.fail("删除失败！");
            return BaseRespVo.error(402,"删除失败");
        }
        return BaseRespVo.ok();
    }

    // TODO:admin:topic:read
}
